﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GameFrameLibrary
{
    /// <summary>
    /// 
    /// </summary>
    public partial class VideoElement : UserControl
    {
        public VideoElement()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(VideoElement_Loaded);
        }

        /// <remarks>位置<1>处注意，GC回收MediaElement时会参照此参数，不要设定为Close以外的MediaState，否则会导致内存溢出。</remarks>
        void VideoElement_Loaded(object sender, RoutedEventArgs e)
        {
            this.MediaElement.LoadedBehavior = MediaState.Manual;

            
            this.MediaElement.UnloadedBehavior = MediaState.Close; //<1>

            this.MediaElement.Play();
        }

        private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (this.IsLoop)
            {
                this.MediaElement.Close();

                this.MediaElement.Play();
            }
        }

        public bool IsLoop { get; set; }

        public Uri Source
        {
            get { return this.MediaElement.Source; }
            set { this.MediaElement.Source = value; }
        }
    }
}
